//
//  LGCancerTreatRecordController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/10.
//  癌症 -- 治疗记录

#import "LGCancerTreatRecordController.h"
//各项治疗记录
#import "LGTreatRecordTableController.h"
#import "LGCancerTreatRecordCell.h"

@interface LGCancerTreatRecordController ()<UITableViewDelegate,UITableViewDataSource,LGCancerTreatRecordDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *itemArray;
/**   */
@property (nonatomic , copy)NSString *typeid;

@end

@implementation LGCancerTreatRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.typeid = @"";
    self.title = @"治疗记录";
    self.levelid = (_levelid && _levelid.length>0)?_levelid:@"";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.tableView frame:CGRectMake(0, viewPix(120), Screen_W, Screen_H-topBarHeight-viewPix(120))];
    [self requestTypeData];
    [self requestData];
}

-(void)requestTypeData{
    [RequestUtil withPOST:@"/doctor/patient.PatientTreatment/treatTypeList" parameters:@{@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            NSMutableArray *itemArray = [NSMutableArray arrayWithArray:returnData];
            [itemArray insertObject:@{@"title":@"全部",@"value":@"0"} atIndex:0];
            self.itemArray = itemArray;
            self.tableView.tableHeaderView = [self tableHeaderView:itemArray];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/patient.PatientTreatment/treatmentRecords" parameters:@{@"patient_id":self.patientid,@"treat_type":self.typeid,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (code == 200) {
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if (self.dataArray.count == 0) {
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无记录" offset:0];
        }
    }];
}

-(void)itemButtonSelect:(UIButton *)sender{
    NSInteger index = sender.tag - 5000;
    for (NSInteger i=0; i<self.itemArray.count; i++) {
        UIButton *button = [self.view viewWithTag:5000+i];
        button.selected = (i==index)?YES:NO;
    }
    self.typeid = (index == 0)?@"":LGNSString(self.itemArray[index][@"value"]);
    [self requestData];
}

//查看治疗记录列表
-(void)checkTreatRecord:(NSDictionary *)dataDic{
    LGTreatRecordTableController *controller = [[LGTreatRecordTableController alloc]init];
    controller.patientid = self.patientid;
    controller.treatType = LGNSString(dataDic[@"treat_type"]);
    controller.title = LGNSString(dataDic[@"treat_type_name"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGCancerTreatRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.topLine.hidden = (indexPath.row == 0)?YES:NO;
    cell.delegate = self;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
    return headView;
}
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGCancerTreatRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIView *)tableHeaderView:(NSArray *)itemArray{
    UIView *baseView = [[UIView alloc]init];
    baseView.backgroundColor = [UIColor clearColor];
    CGFloat leftMargin = viewPix(16);
    CGFloat topMargin = viewPix(16);
    CGFloat baseHeight = 0;
    CGFloat maxWidth = Screen_W-viewPix(32);
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSString *content = LGNSString(itemArray[i][@"title"]);
        CGFloat contentW = [content textWidthWithFont:LGFontWeight(16, UIFontWeightMedium) maxHeight:MAXFLOAT]+viewPix(30);
        topMargin = (leftMargin+contentW)>maxWidth?topMargin+viewPix(42):topMargin;
        leftMargin = (leftMargin+contentW)>maxWidth?viewPix(16):leftMargin;
        UIButton *itemButton = [UIButton buttonWithTitle:content titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#909399" frame:CGRectMake(leftMargin, topMargin, contentW, viewPix(32))];
        [itemButton setBackgroundColor:[UIColor colorWithString:@"#FFFFFF"] forState:UIControlStateNormal];
        [itemButton setSelectTextColor:@"#FFFFFF" backColor:kThemeColor];
        itemButton.cornerRidus = viewPix(16);
        itemButton.tag = 5000+i;
        itemButton.selected = (i == 0)?YES:NO;
        [itemButton addTarget:self action:@selector(itemButtonSelect:) forControlEvents:UIControlEventTouchUpInside];
        [baseView addSubview:itemButton];
        leftMargin = kMaxX(itemButton.frame)+viewPix(10);
        baseHeight = kMaxY(itemButton.frame);
    }
    baseView.frame = CGRectMake(0, 0, Screen_W, baseHeight+viewPix(5));
    
    return baseView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
